package com.tanhua.server.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.utils.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@ControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice {

    //缓存全局开关
    @Value("${tanhua.cache.enable}")
    private boolean enable;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private static final ObjectMapper MAPPER=new ObjectMapper();


    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        //开关处于开启状态  是get请求 包含了@cache注解
        return enable&&returnType.hasMethodAnnotation(GetMapping.class)
                &&returnType.hasMethodAnnotation(Cache.class);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        if (body==null){
            return null;
        }
        try {

            String redisValue=null;
            if (body instanceof String){
                redisValue=(String)body;
            }else {
                redisValue=MAPPER.writeValueAsString(body);
            }

            String redisKey = RedisCacheInterceptor.createRedisKey(((ServletServerHttpRequest) request).getServletRequest());

            Cache cache = returnType.getMethodAnnotation(Cache.class);
            //缓存时间单位是秒
            this.redisTemplate.opsForValue().set(redisKey,redisValue,Long.valueOf(cache.time()), TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return body;
    }
}
